﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace CommSvcCreator
{
    class FileUtil
    {
        /// <summary>
        /// 从资源文件中抽取资源文件
        /// </summary>
        /// <param name="resFileName">资源文件名称（资源文件名称必须包含目录，目录间用“.”隔开,最外层是项目默认命名空间）</param>
        /// <param name="outputFile">输出文件</param>
        public static void ExtractResFile(string resFileName, string outputFile)
        {
            BufferedStream inStream = null;
            FileStream outStream = null;
            try
            {
                Assembly asm = Assembly.GetExecutingAssembly(); //读取嵌入式资源
                inStream = new BufferedStream(asm.GetManifestResourceStream(resFileName));
                outStream = new FileStream(outputFile, FileMode.Create, FileAccess.Write);

                byte[] buffer = new byte[1024];
                int length;

                while ((length = inStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outStream.Write(buffer, 0, length);
                }
                outStream.Flush();
            }
            catch(Exception ex){
                Console.WriteLine("ex.tostring=>" + ex.ToString());
                Console.Read();
            }
            finally
            {
                if (outStream != null)
                {
                    outStream.Close();
                }
                if (inStream != null)
                {
                    inStream.Close();
                }
            }
        }

        public static string[] EnumAllResources(){
            Assembly asm = Assembly.GetExecutingAssembly(); //读取嵌入式资源
            string[] allNames = asm.GetManifestResourceNames();
            
            return allNames;
        }

        public static string GetMainZipName(){
            string[] arr = EnumAllResources();
            string zipName = "";
            foreach(string a in arr){
                if(a.EndsWith(".zip") && !a.EndsWith("source.zip"))
                {
                    zipName = a.Substring(0, a.Length - 4);
                    int index = zipName.LastIndexOf(".");
                    zipName = zipName.Substring(index + 1);
                }
            }

            return zipName;
        }

        public static int GetZipFileCount(){
            int counter = 0;
            string[] arr = EnumAllResources();
            foreach(string a in arr)
            {
                if(a.EndsWith(".zip") && !a.EndsWith("source.zip"))
                {
                    counter++;
                }
            }

            return counter;
        }

        public static string extract2TempPath(string resName){
            string tempPath = Path.GetTempPath() + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss");
            if (!Directory.Exists(tempPath))
            {
                Directory.CreateDirectory(tempPath);
            }
            Console.WriteLine("解压路径=>" + tempPath);
            string res_zip_file = tempPath + "\\"+ resName + ".zip";
            FileUtil.ExtractResFile("CommSvcCreator.resc."+ resName + ".zip", res_zip_file);
            using (var unzip = new Unzip(res_zip_file))
            {
                try
                {
                    unzip.ExtractToDirectory(tempPath);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("解压出错=>" + ex.ToString());
                }
            }

            return tempPath;
        }
    }
}
